<?php

namespace app\index\controller;

use think\Request;
use think\Config;
use app\index\service\FaceRecognitionService;
use app\common\controller\Base;
use app\common\common\Constants;
use app\common\service\OssService;

/**
 * 百度人脸识别相关
 *
 * @author jiang.ding
 */
class FaceRecognition extends Base {

    /**
     * 初始化方法
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 添加会员授权图片 [OK]
     */
    public function addGrantPic() {
        
        try {
            $params = $this->input();
            $params['companyId'] = $this->getCompanyIdByCompanyCode($params['companyCode']);

            $service = new FaceRecognitionService($params['companyId']);
            $addGrantRes = $service->addMemberGrantFace($params['url'],
                    $params['companyId'], $params['memberCode']);
//var_dump($addGrantRes);die;
            return $this->output($addGrantRes);
        } catch (\Exception $ex) {
            return $this->output([], $ex->getMessage(), 200);
        }
    }

    /**
     * 解绑会员授权图片 [OK]
     */
    public function cancelMemberGrantPic() {

        try {
            $params = $this->input();
            $params['companyId'] = $this->getCompanyIdByCompanyCode($params['companyCode']);

            $service = new FaceRecognitionService($params['companyId']);
            $cancelGrantRes = $service->cancelMemberGrantInfo($params['memberCode']);
            return $this->output($cancelGrantRes);
        } catch (\Exception $ex) {
            return $this->output([], $ex->getMessage(), 200);
        }
    }

    /**
     * 当天到店会员列表 (标明门店ID) [OK]
     */
    public function getArriveMemberList() {

        try {
            $params = $this->input();
            $params['companyId'] = $this->getCompanyIdByCompanyCode($params['companyCode']);

            $service = new FaceRecognitionService($params['companyId']);
//            $arriveListRes = $service->getArriveMemberList(['store_id' => 'JJ_' . $params['storeId']]);
            $arriveListRes = $service->getArriveMemberList(['store_id' => $params['storeId']]);
//            var_dump($arriveListRes);die;
            return $this->output($arriveListRes);
        } catch (\Exception $ex) {

            return $this->output([], $ex->getMessage(), 200);
        }
    }

    /**
     * 获取会员授权信息
     */
    public function getMemberGrantInfo() {

        try {

            $params = $this->input();
            $params['companyId'] = $this->getCompanyIdByCompanyCode($params['companyCode']);

            $service = new FaceRecognitionService($params['companyId']);
            $where['member_code'] = $params['memberCode'];
            $where['grant_id'] = $params['grantId'];
            $grantInfoRes = $service->getMemberGrantInfo($where);

            # 获取带有签名的url
            $getSignData = [
                'bucket' => Config::get('ALIYUN_OSS_FACE_GRANT_BUCKET'),
                'object' => $grantInfoRes->grant_pic,
            ];
            # 创建OSS的bucket对象
            $ossServiceGrant = new OssService(Config::get('ALIYUN_OSS_FACE_GRANT_BUCKET'));
            $filePath = $ossServiceGrant->getSignUrl($getSignData);
            return $this->output($filePath);
        } catch (\Exception $ex) {
            return $this->output([], $ex->getMessage(), 200);
        }
    }

    /**
     * 撤销到店人脸识别 [OK]
     */
    public function cancelArriveMember() {

        try {

            $params = $this->input();
            $params['companyId'] = $this->getCompanyIdByCompanyCode($params['companyCode']);

            $service = new FaceRecognitionService($params['companyId']);
            $where = [
                'member_code' => $params['memberCode'],
                'grant_id' => $params['grantId'],
//                'store_id' => 'JJ_' . $params['storeId'],
                'store_id' => $params['storeId'],
                'cancel_reason' => $params['cancelReason'],
            ];
            $cancelMemberArriveRes = $service->cancelArriveMember($where);
            return $this->output($cancelMemberArriveRes);
        } catch (\Exception $ex) {
            return $this->output([], $ex->getMessage(), 200);
        }
    }

    /**
     * 添加到店会员签到 [OK]
     */
    public function addArriveMemberSign() {

        try {
            $params = $this->input(); //6个参数
            $params['companyId'] = $this->getCompanyIdByCompanyCode($params['companyCode']);

            $service = new FaceRecognitionService($params['companyId']);
            # 先插入签到表, 软删除到店记录
            $addSignData = [
                'member_code' => $params['memberCode'],
//                'vip_id' => $params['vipId'],
                'service_p' => $params['staffId'],
                'service_store' => $params['storeId'],
                'come_from' => 3, //到店
            ];
            $addArriveSignRes = $service->addArriveMemberSign($addSignData,
                    $params['companyCode']);
            return $this->output($addArriveSignRes);
        } catch (\Exception $ex) {

            return $this->output([], $ex->getMessage(), 200);
        }
    }

    /**
     * 获取签名的方法 [目前只用于测试]
     */
    public function getSignUrl() {
        $param = Request::instance()->param();
//        $params = $this->input();
        # 获取带有签名的url
        $getSignData = [
            'bucket' => Config::get('ALIYUN_OSS_FACE_CAMERA_BUCKET'),
            'object' => $param['path'],
        ];
        # 创建OSS的bucket对象
        $ossServiceGrant = new OssService(Config::get('ALIYUN_OSS_FACE_CAMERA_BUCKET'));
        $picPath = $ossServiceGrant->getSignUrl($getSignData);
        var_dump($picPath);
        die;
    }

    /**
     * 查询签到记录(测试用)
     */
    public function getMemberSignInfo() {
        $params = Request::instance()->param();
//        $params = $this->input();
        $service = new FaceRecognitionService($params['company_id']);
        $where = [
            'service_store' => $params['store_id'],
            'member_code' => $params['member_code'],
        ];
        $res = $service->getMemberSignInfo($where);
        var_dump(date('Y-m-d H:i:s', $res->add_ts));
    }

    /**
     * BaiduFaceBase的软删除(测试用)
     */
    public function baseTest() {
        $memberCode = '15559009';
        $companyId = 1;

        # 查询表
//        $grant = new \app\common\model\jooj_member\MemberFaceGrant($companyId);
        $arrive = new \app\common\model\jooj_member\MemberFaceArrive($companyId);
        $face = new \app\common\model\jooj_member\MemberFace($companyId);


//        $grantRes = $grant->where('grant_id','>=','1')->fetchSql()->select();
        $arriveRes = $arrive->where('arrive_id', '>=', '1')->fetchSql()->select();
//        var_dump($grantRes);echo 3333333;
//        var_dump($arriveRes);die;
        # 软删除表
//        $grantDel = $grant->where(['member_code' => $memberCode])->order('create_time','desc')->find();
//        $grant->restore(['grant_id'=>70]);die;
//        $grantDelRes = $grantDel->delete();
//        $arriveDel = $arrive->where(['member_code' => $memberCode])->find();
        $arrive->restore(['arrive_id' => 15]);
        die; //恢复软删除数据(pk)
//        $arriveDelRes = $arriveDel->delete();
        var_dump($grantDelRes);
    }

    public function test() {
        
        $snName = '1|FACE_CAMERA|zero-1-2018';
        $pos = strrpos($snName,'-');
        $sodSnName = substr($snName,0,$pos);
        $data['sodSnName'] = $sodSnName;
        $data['companyId'] = explode('|',$sodSnName)[0];
        $service = new FaceRecognitionService(1);
        $storeIdRes = $service->getStoreIdBySodSnName($data)['store_id'];
        var_dump($storeIdRes);die;
       
    }

}
